package org.davidtrevien.com.imput.omi.panels.center.planning.model;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;

import org.davidtrevien.com.imput.omi.panels.center.planning.TableEditionEvent;
import org.davidtrevien.com.imput.omi.panels.center.planning.TableEditionListener;
import org.jdesktop.swingx.treetable.DefaultMutableTreeTableNode;
import org.jdesktop.swingx.treetable.DefaultTreeTableModel;
import org.jdesktop.swingx.treetable.TreeTableNode;

public class TOsDaysModel 
	extends DefaultTreeTableModel {

	private static final int NAME = 0;
	private static final int DAY1 = 1;
	private final List<TableEditionListener> tableEditionListeners = new ArrayList<TableEditionListener>();

	private final int columnCount;
	
	private int currentYear = 2014;

	public TOsDaysModel(TreeTableNode root) {
		super(root);
		this.columnCount = 366+DAY1;
	}
	
	@Override
	public int getColumnCount() {
		return this.columnCount;
	}
	
	/**
	 * Returns which object is displayed in this column.
	 */
	@Override
	public Object getValueAt(Object node, int column) {
		Object res = "";
		if (node instanceof DefaultMutableTreeTableNode) {
			DefaultMutableTreeTableNode defNode = (DefaultMutableTreeTableNode) node;
			if (defNode.getUserObject() instanceof TTask) {
				TTask ttask = (TTask) defNode.getUserObject();
				if (column < DAY1) {
					switch (column) {
					case NAME:
						res = ttask.getTos().getName();
						break;

					default:
						break;
					}
				}
				else {
					float v = 0;
					if (column-DAY1 < ttask.days.length) {
						v = ttask.days[column-DAY1];
					}
					if (v == 0) {
						res = "";
					}
					else {
						res = v;
					}
				}
			}
		}
		return res;
	}
	
	/**
	 * What the TableHeader displays when the Table is in a JScrollPane.
	 */
	@Override
	public String getColumnName(int column) {
		String res = "";
		
		int currentDayOfYear = column-DAY1;
		Calendar gmtCalendar = new GregorianCalendar(this.currentYear, 0, 1);
		gmtCalendar.add(Calendar.DATE, currentDayOfYear);
		
		if (gmtCalendar.get(Calendar.YEAR) > this.currentYear) {
			res = "";
		}
		else {
			res = gmtCalendar.get(Calendar.DAY_OF_MONTH)+"/"+(gmtCalendar.get(Calendar.MONTH)+1);
		}
		
		return res;
	}

	/**
	 * Tells if a column can be edited.
	 */
	@Override
	public boolean isCellEditable(Object node, int column) {
		return true;
	}

	/**
	 * Called when done editing a cell.
	 */
	@Override
	public void setValueAt(Object value, Object node, int column) {
		if (node instanceof DefaultMutableTreeTableNode) {
			DefaultMutableTreeTableNode defNode = (DefaultMutableTreeTableNode) node;
			if (defNode.getUserObject() instanceof TTask) {
				TTask ttask = (TTask) defNode.getUserObject();
				if (!value.toString().isEmpty()) {
					try {
						ttask.setDay(column-DAY1, Float.parseFloat(value.toString()));
					}
					catch (NumberFormatException e) {}
				}
				else {
					ttask.setDay(column-DAY1, 0);
				}
				
				for (TableEditionListener l : this.tableEditionListeners) {
					TableEditionEvent e = new TableEditionEvent();
					e.setSource(ttask);
					l.valueChanged(e);
				}
			}
		}
	}
	
	public void addTableEditionListener(
			TableEditionListener tableEditionListener) {
		this.tableEditionListeners .add(tableEditionListener);
	}

	public int getCurrentYear() {
		return this.currentYear;
	}

	public void setCurrentYear(int currentYear) {
		this.currentYear = currentYear;
	}
}
